package com.mashibing.tank;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;

/**
 * Copyright (C) 2024 - 2024 Jasonakeke， Inc. All Rights Reserved
 *
 * @Desc :
 * @Time : 2024/10/3 16:58
 * @Author : Code_By_Jasonakeke
 * @Email : 2284037977@qq.com
 * @Class : Tank
 * @IDE : IntelliJ IDEA
 */
public class Tank {

	private int x;

	private int y;

	private Dir dir = Dir.DOWN;

	public static final int SPEED = Integer.parseInt((String) PropertyMgr.get("tankSpeed"));

	private static int WIDTH = ResourceMgr.goodTankU.getWidth();

	private static int HEIGHT = ResourceMgr.goodTankU.getHeight();

	private Rectangle rect = new Rectangle();

	private Random random = new Random();

	private boolean moving = true;

	private boolean living = true;

	private TankFrame tankFrame = null;

	private Group group = Group.BAD;

	public Tank(int x, int y, Dir dir, Group group, TankFrame tankFrame) {
		this.x = x;
		this.y = y;
		this.dir = dir;
		this.group = group;
		this.tankFrame = tankFrame;

		rect.x = x;
		rect.y = y;
		rect.width = WIDTH;
		rect.height = HEIGHT;
	}

	public void paint(Graphics g) {
		if (!living) {
			tankFrame.tanks.remove(this);
		}

		switch (dir) {
			case LEFT:
				g.drawImage(group == Group.GOOD ? ResourceMgr.goodTankL : ResourceMgr.badTankL, x, y, null);
				break;
			case UP:
				g.drawImage(group == Group.GOOD ? ResourceMgr.goodTankU : ResourceMgr.badTankU, x, y, null);
				break;
			case RIGHT:
				g.drawImage(group == Group.GOOD ? ResourceMgr.goodTankR : ResourceMgr.badTankR, x, y, null);
				break;
			case DOWN:
				g.drawImage(group == Group.GOOD ? ResourceMgr.goodTankD : ResourceMgr.badTankD, x, y, null);
				break;
		}
		move();

	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public static int getWIDTH() {
		return WIDTH;
	}

	public static void setWIDTH(int WIDTH) {
		Tank.WIDTH = WIDTH;
	}

	public static int getHEIGHT() {
		return HEIGHT;
	}

	public static void setHEIGHT(int HEIGHT) {
		Tank.HEIGHT = HEIGHT;
	}

	public TankFrame getTankFrame() {
		return tankFrame;
	}

	public Rectangle getRect() {
		return rect;
	}

	public void setRect(Rectangle rect) {
		this.rect = rect;
	}

	public void setTankFrame(TankFrame tankFrame) {
		this.tankFrame = tankFrame;
	}

	public Dir getDir() {
		return dir;
	}

	public void setDir(Dir dir) {
		this.dir = dir;
	}

	public boolean isMoving() {
		return moving;
	}

	public void setMoving(boolean moving) {
		this.moving = moving;
	}

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public void move() {
		if (!moving) {
			return;
		}
		switch (dir) {
			case LEFT:
				x -= SPEED;
				break;
			case RIGHT:
				x += SPEED;
				break;
			case UP:
				y -= SPEED;
				break;
			case DOWN:
				y += SPEED;
				break;
			default:
				break;
		}

		if (group == Group.BAD && random.nextInt(100) > 95) {
			fire();
		}
		if (group == Group.BAD && random.nextInt(100) > 95) {
			randomDir();
		}
		boundsCheck();
		// update rect
		rect.x = x;
		rect.y = y;
	}

	private void boundsCheck() {
		if (x < 2) {
			x = 2;
		}
		if (y < 28) {
			y = 28;
		}
		if (x > TankFrame.GAME_WIDTH - Tank.getWIDTH() - 2) {
			x = TankFrame.GAME_WIDTH - Tank.getWIDTH() - 2;
		}
		if (y > TankFrame.GAME_HEIGHT - Tank.getHEIGHT() - 2) {
			y = TankFrame.GAME_HEIGHT - Tank.getHEIGHT() - 2;
		}
	}

	private void randomDir() {
		dir = Dir.values()[random.nextInt(4)];
	}


	public void fire() {
		int bX = x + WIDTH / 2 - Bullet.WIDTH / 2;
		int bY = y + HEIGHT / 2 - Bullet.HEIGHT / 2;
		tankFrame.bullets.add(new Bullet(bX, bY, dir, group, tankFrame));
	}

	public void die() {
		living = false;
	}
}
